package com.jz.community.moduleshopping.shopCart.ui;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.baseGoods.BaseGoodsInfo;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.shopCart.adapter.TogetherGoodsAdapter;
import com.jz.community.moduleshopping.shopCart.task.GetTogetherGoodsTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Tony.Fan on 2018/10/12 09:00
 * 购物车 凑单商品
 */
public class PieceTogetherGoodsActivity extends BaseMvpActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_right_iv)
    ImageView titleRightIv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.channel_out_comprehensive_btn)
    TextView channelOutComprehensiveBtn;
    @BindView(R2.id.channel_out_price_tv)
    TextView channelOutPriceTv;
    @BindView(R2.id.channel_out_price_iv)
    ImageView channelOutPriceIv;
    @BindView(R2.id.channel_out_price_btn)
    LinearLayout channelOutPriceBtn;
    @BindView(R2.id.channel_out_sale_tv)
    TextView channelOutSaleTv;
    @BindView(R2.id.channel_out_sale_iv)
    ImageView channelOutSaleIv;
    @BindView(R2.id.channel_out_sale_btn)
    LinearLayout channelOutSaleBtn;
    @BindView(R2.id.channel_out_new_btn)
    TextView channelOutNewBtn;
    @BindView(R2.id.together_goods_rv)
    RecyclerView togetherGoodsRv;
    @BindView(R2.id.together_refresh)
    SmartRefreshLayout togetherRefresh;
    @BindView(R2.id.channel_out_parent)
    LinearLayout channelOutParent;
    private String shopId;
    private String salePrice;
    private String sort = "";
    private int page = 0;

    private TogetherGoodsAdapter togetherGoodsAdapter;
    private BaseGoodsInfo baseGoodsInfo;

    private int priceUpOrDown = 1;//0 箭头向上（降序） 1 箭头向下（升序）
    private int saleUpOrDown = 0;//0 箭头向上（降序） 1 箭头向下（升序）

    @Override
    protected int getContentView() {
        return R.layout.together_goods_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        titleName.setText("凑单商品");
        setImmersionBar(titleToolbar);
        shopId = getIntent().getStringExtra("shopId");
        salePrice = getIntent().getStringExtra("salePrice");
        titleBackLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleBindAdapter();
        getTogetherGoodsData(true);
        setTogetherAllDefault();
    }

    private void setTogetherAllDefault() {
        channelOutComprehensiveBtn.setEnabled(false);
        channelOutComprehensiveBtn.setTextColor(getResources().getColor(R.color.home_red));
    }

    private void getTogetherGoodsData(boolean isRefresh) {
        new GetTogetherGoodsTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                baseGoodsInfo = (BaseGoodsInfo) o;
                if (!Preconditions.isNullOrEmpty(baseGoodsInfo.get_embedded())) {
                    setData(isRefresh, baseGoodsInfo.get_embedded().getContent());
                    togetherRefresh.finishRefresh();
                } else {
                    togetherRefresh.finishRefresh();
                    togetherGoodsAdapter.setEmptyView(noDataView(togetherGoodsRv, "暂无商品！"));
                }
            }
        }, isRefresh).execute(page + "", shopId, sort, salePrice);
    }

    private void handleBindAdapter() {
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 2);
        togetherRefresh.setOnRefreshListener(this);
        togetherGoodsRv.setLayoutManager(layoutManager);
        togetherGoodsAdapter = new TogetherGoodsAdapter(R.layout.together_goods_item_layout, new ArrayList<>());
        togetherGoodsAdapter.setEnableLoadMore(false);
        togetherGoodsRv.setAdapter(togetherGoodsAdapter);
    }

    @Override
    public void onLoadMoreRequested() {
        getTogetherGoodsData(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        getTogetherGoodsData(false);
        togetherGoodsAdapter.setEnableLoadMore(false);
    }

    private void setData(boolean isRefresh, List data) {

        page++;

        if (isRefresh) {
            togetherGoodsAdapter.setNewData(data);
        } else {
            togetherGoodsAdapter.addData(data);
        }

        if (baseGoodsInfo.getPage().getTotalPages() <= page) {
            togetherGoodsAdapter.loadMoreEnd();
        } else {
            togetherGoodsAdapter.loadMoreComplete();
        }
    }

    @OnClick(R2.id.channel_out_comprehensive_btn)
    public void comClick(View view) {
        //综合
        channelOutComprehensiveBtn.setEnabled(false);
        channelOutComprehensiveBtn.setTextColor(getResources().getColor(R.color.home_red));

        //价格变更
        priceUpOrDown = 1;//当点击到其他时，再点击价格选项，要置为向上箭头
        channelOutPriceTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutPriceIv.setImageResource(R.mipmap.sr_normal);
        channelOutPriceBtn.setEnabled(true);

        //销量变更
        saleUpOrDown = 0;//当点击到其他时，再点击销量选项，要置为向下箭头
        channelOutSaleTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutSaleIv.setImageResource(R.mipmap.sr_normal);
        channelOutSaleBtn.setEnabled(true);

        //上新变更
        channelOutNewBtn.setTextColor(getResources().getColor(R.color.font_black));
        channelOutNewBtn.setEnabled(true);
        sort = "";
        page = 0;
        getTogetherGoodsData(true);
    }

    @OnClick(R2.id.channel_out_price_btn)
    public void priceClick(View view) {
        //价格
        channelOutPriceTv.setTextColor(getResources().getColor(R.color.home_red));
        if (priceUpOrDown == 0) {
            priceUpOrDown = 1;
            sort = "pricedesc";
            channelOutPriceIv.setImageResource(R.mipmap.sr_down);
        } else {
            priceUpOrDown = 0;
            sort = "priceasc";
            channelOutPriceIv.setImageResource(R.mipmap.sr_up);
        }

        //综合变更
        channelOutComprehensiveBtn.setTextColor(getResources().getColor(R.color.font_gary));
        channelOutComprehensiveBtn.setEnabled(true);

        //销量变更
        saleUpOrDown = 0;//当点击到其他时，再点击销量选项，要置为向下箭头
        channelOutSaleTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutSaleIv.setImageResource(R.mipmap.sr_normal);
        channelOutSaleBtn.setEnabled(true);

        //上新变更
        channelOutNewBtn.setTextColor(getResources().getColor(R.color.font_black));
        channelOutNewBtn.setEnabled(true);
        page = 0;
        getTogetherGoodsData(true);
    }

    @OnClick(R2.id.channel_out_sale_btn)
    public void saleClick(View view) {
        //销量
        channelOutSaleTv.setTextColor(getResources().getColor(R.color.home_red));
        if (saleUpOrDown == 0) {
            saleUpOrDown = 1;
            sort = "saledesc";
            channelOutSaleIv.setImageResource(R.mipmap.sr_down);
        } else {
            saleUpOrDown = 0;
            sort = "saleasc";
            channelOutSaleIv.setImageResource(R.mipmap.sr_up);
        }

        //综合变更
        channelOutComprehensiveBtn.setTextColor(getResources().getColor(R.color.font_gary));
        channelOutComprehensiveBtn.setEnabled(true);

        //价格变更
        priceUpOrDown = 1;//当点击到其他时，再点击价格选项，要置为向上箭头
        channelOutPriceTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutPriceIv.setImageResource(R.mipmap.sr_normal);
        channelOutPriceBtn.setEnabled(true);

        //上新变更
        channelOutNewBtn.setTextColor(getResources().getColor(R.color.font_black));
        channelOutNewBtn.setEnabled(true);

        page = 0;
        getTogetherGoodsData(true);

    }

    @OnClick(R2.id.channel_out_new_btn)
    public void newClick(View view) {
        //上新
        channelOutNewBtn.setEnabled(false);
        channelOutNewBtn.setTextColor(getResources().getColor(R.color.home_red));

        //综合变更
        channelOutComprehensiveBtn.setTextColor(getResources().getColor(R.color.font_black));
        channelOutComprehensiveBtn.setEnabled(true);

        //价格变更
        priceUpOrDown = 1;//当点击到其他时，再点击价格选项，要置为向上箭头
        channelOutPriceTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutPriceIv.setImageResource(R.mipmap.sr_normal);
        channelOutPriceBtn.setEnabled(true);

        //销量变更
        saleUpOrDown = 0;//当点击到其他时，再点击销量选项，要置为向下箭头
        channelOutSaleTv.setTextColor(getResources().getColor(R.color.font_black));
        channelOutSaleIv.setImageResource(R.mipmap.sr_normal);
        channelOutSaleBtn.setEnabled(true);

        sort = "createddesc";
        page = 0;
        getTogetherGoodsData(true);
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }
}
